package _动态规划系列._股票买卖问题;

/**
 * @Author: 吕庆龙
 * @Date: 2020/3/31 16:38
 * <p>
 * 功能描述:
 */
public class Summary_0309 {

    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][2];
        if (n == 0) return 0;

        //base case
//        dp[0][0] = Math.max(dp[-1][0], dp[-1][1] + prices[i])
//                 = Math.max(0, Integer.MIN_VALUE + prices[i])
//                 = 0;
//        dp[0][1] = Math.max(dp[-1][1], dp[-1][0] - prices[i])
//                 = Math.max(Integer.MIN_VALUE, 0 - prices[i])
//                 = -prices[i];

        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            if (i - 2 == -1) {
                dp[i][1] = Math.max(-prices[0],-prices[i]);
                continue;
            }
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 2][0] - prices[i]);
        }
        return dp[n - 1][0];
    }
}
